package study.cell_game.objects;

import java.util.ArrayList;
import java.util.List;

import org.andengine.entity.scene.ITouchArea;
import org.andengine.input.touch.TouchEvent;

import study.cell_game.activity.GameActivity;
import study.cell_game.enums.Direction;
import study.cell_game.objects.textures.TokenTextures;
import study.cell_game.objects.tokens.Token;
import study.cell_game.objects.tokens.TokenSprite;
import study.cell_game.scenes.GameScene;
import study.cell_game.utils.TapManager;

/**
 * @author bherman
 *
 */
public class Cell {//implements ITouchArea{
	
	private Token token;
	
	private Cell[] neighbours = new Cell[6];
	
	CellSprite sprite;
	
	private List<Direction> outboundDirections;
	
	//współrzędne
	int X;
	int Y;
	
	//Określa, czy to komórka mapy
	private boolean mapCell = false;
	
	public Cell(){
		outboundDirections = new ArrayList<Direction>();
	}
	
	public Cell(int X, int Y){
		outboundDirections = new ArrayList<Direction>();
		
		this.X = X;
		this.Y = Y;
	}
	
	public void init(){
		sprite = new CellSprite(X, Y, TokenTextures.getInstance().getField(),
				GameActivity.main.getVertexBufferObjectManager(), this);
		
		
	}
	
	public int getX(){
		return X;
	}
	
	public int getY(){
		return Y;
	}
	
	public void tokenPlaced(){
		for(Direction dir : Direction.values()){
			if (neighbours[dir.getValue()]!= null && neighbours[dir.getValue()].getToken() != null){
				Token otherToken = neighbours[dir.getValue()].getToken();
				if(otherToken.getBorder().canEnterVia(dir.opposite()) && this.token.getBorder().canLeaveVia(dir)){
					outboundDirections.add(dir);
				}
				if(otherToken.getBorder().canLeaveVia(dir.opposite()) && this.token.getBorder().canEnterVia(dir)){
					neighbours[dir.getValue()].outboundDirections.add(dir.opposite());
				}
				
			}
		}
	}
	
	
	
	
	
	public CellSprite getSprite() {
		return sprite;
	}

	public void setSprite(CellSprite sprite) {
		this.sprite = sprite;
	}

	public List<Direction> getOutboundDirections() {
		return outboundDirections;
	}

	public void setOutboundDirections(List<Direction> outboundDirections) {
		this.outboundDirections = outboundDirections;
	}

	public Cell[] getNeighbours() {
		return neighbours;
	}

	public void setNeighbours(Cell[] neighbours) {
		this.neighbours = neighbours;
	}

	public Token getToken() {
		return token;
	}

	public void setToken(Token token) {
		this.token = token;
		tokenPlaced();
	}

//	@Override
//	public boolean contains(float pX, float pY) {
//		// TODO Auto-generated method stub
//		return false;
//	}
//
//	@Override
//	public float[] convertSceneToLocalCoordinates(float pX, float pY) {
//		float result[] = new float[]{pX - X, pY - Y};
//		return result;
//	}
//
//	@Override
//	public float[] convertLocalToSceneCoordinates(float pX, float pY) {
//		float result[] = new float[]{X + pX, Y + pY};
//		return result;
//	}
//
//	@Override
//	public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
//		if(TouchEvent.ACTION_DOWN == (pSceneTouchEvent.getAction())){
//			TapManager.getInstance().tap(this);
//		}
//
//		return true;
//	}

	public void setMapCell(boolean mapCell){
		this.mapCell = mapCell;
	}
	
	public boolean isMapCell() {
		return this.mapCell;
	}
}
